﻿@using System.Diagnostics.CodeAnalysis
@using JxAudio.Core
@using JxAudio.Core.Enums
@using JxAudio.Core.Options
@using JxAudio.Web.Validators
@using Microsoft.Extensions.Localization
@inherits StepBase
@inject SwalService SwalService
@inject IStringLocalizer<DbSetup> DbStringLocalizer

<ValidateForm Model="@DbConfig" OnValidSubmit="@OnSubmit">
    <GroupBox class="install_content">
        <div class="row g-3">
            <div class="form-group col-12">
                <Select @bind-Value="DbConfig.DbType" Items="typeof(DbType).ToSelectList()" OnSelectedItemChanged="OnSelectedItemChanged"></Select>
            </div>
            @if (_needShow)
            {
                <div class="form-group col-12">
                    <BootstrapInput @bind-Value="@DbConfig.DbUrl">
                    </BootstrapInput>
                </div>
                <div class="form-group col-12">
                    <BootstrapInput @bind-Value="@DbConfig.DbPort" type="number">
                    </BootstrapInput>
                </div>
            }
            
            <div class="form-group col-12">
                <BootstrapInput @bind-Value="@DbConfig.DbName"></BootstrapInput>
            </div>
            @if (_needShow)
            {
                <div class="form-group col-12">
                    <BootstrapInput @bind-Value="@DbConfig.Username">
                    </BootstrapInput>
                </div>
                <div class="form-group col-12">
                    <BootstrapInput @bind-Value="@DbConfig.Password" type="password">
                    </BootstrapInput>
                </div>
            }
            
            <div class="form-group col-12">
                <BootstrapInput @bind-Value="@DbConfig.Prefix"></BootstrapInput>
            </div>
        </div>
    </GroupBox>
    <div class="text-center">
        <Button Text="上一步" OnClick="() => Prev?.Invoke()"></Button>
        <Button ButtonType="ButtonType.Submit">验证并继续</Button>
    </div>
</ValidateForm>

@code {
    [NotNull]
    [Parameter]
    public DbConfigOption? DbConfig { get; set; }
    
    private bool _needShow = false;
    
    private async Task OnSubmit(EditContext arg)
    {
        var ret = Setup.SetupDb(DbConfig);
        if (!ret.isSuccess)
        {
            await SwalService.Show(new SwalOption()
            {
                Category = SwalCategory.Error,
                Title = DbStringLocalizer["Fail"],
                Content = ret.msg
            });
            return;
        }
        Next?.Invoke();
    }
    
    private Task OnSelectedItemChanged(SelectedItem arg)
    {
        if (Enum.TryParse(arg.Text, true, out DbType dbType))
        {
            switch (dbType)
            {
                case DbType.Sqlite:
                    _needShow = false;
                    DbConfig.DbUrl = null;
                    DbConfig.DbPort = null;
                    DbConfig.Username = null;
                    DbConfig.Password = null;
                    DbConfig.DbName = $"{Application.WebHostEnvironment.ContentRootPath}/config/JxAudio.db";
                    break;
                case DbType.Mysql:
                    _needShow = true;
                    DbConfig.DbPort = "3306";
                    DbConfig.DbName = "JxAudio";
                    break;
                case DbType.SqlServer:
                    _needShow = true;
                    DbConfig.DbPort = "1433";
                    DbConfig.DbName = "JxAudio";
                    break;
                case DbType.Oracle:
                    _needShow = true;
                    DbConfig.DbPort = "1521";
                    DbConfig.DbName = "JxAudio";
                    break;
                case DbType.Postgresql:
                    _needShow = true;
                    DbConfig.DbPort = "5432";
                    DbConfig.DbName = "JxAudio";
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
        return Task.CompletedTask;
    }

}